﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TapTap.Common;
using UnityEngine;

namespace TapTap.Achievement
{
    public interface IAchievementCallback
    {
        void OnAchievementSDKInitSuccess();

        void OnAchievementSDKInitFail(TapError error);

        void OnAchievementStatusUpdate(TapAchievementBean bean, TapError error);
    }
    
    public class TapAchievement
    {
        public const string ACHIEVEMENT_DIR = "tap-achievement";
        public const string ALL_ACHIEVEMENT_DATA = "all-config";
        public const string USER_ACHIEVEMENT_DATA = "user-config";
        public const string ApplictionIconFolder = "TapTap/Achievement/AutoGenerated/Resources";
        public const string IconName = "application_icon";
        public const string ApplictionIconName = IconName + ".png";
        
        private static ITapAchievement platformWrapper;
        
        static TapAchievement()
        {
            Type interfaceType = typeof(ITapAchievement);
            Type platformDBType = AppDomain.CurrentDomain.GetAssemblies()
                .Where(assembly => assembly.GetName().FullName.StartsWith("TapTap.Achievement"))
                .SelectMany(assembly => assembly.GetTypes())
                .SingleOrDefault(clazz => interfaceType.IsAssignableFrom(clazz) && clazz.IsClass);
            if (platformDBType != null) 
            {
                platformWrapper = Activator.CreateInstance(platformDBType) as ITapAchievement;
            }
            else 
            {
                TapLogger.Error($"No class implements {interfaceType} Type. Current Platform: {Application.platform}, if you are using Editor, please check if you have installed pc module.");
            }
        }
        
        public static void Init(TapConfig config) {
            platformWrapper.Init(config);
        }

        public static void RegisterCallback(IAchievementCallback callback) {
            platformWrapper.RegisterCallback(callback);
        }

        public static void InitData() {
            platformWrapper.InitData();
        }

        public static void FetchAllAchievementList(Action<List<TapAchievementBean>, TapError> action)
        {
            platformWrapper.FetchAllAchievementList(action);
        }

        public static void GetLocalAllAchievementList(Action<List<TapAchievementBean>, TapError> action) {
            platformWrapper.GetLocalAllAchievementList(action);
        }


        public static void GetLocalUserAchievementList(Action<List<TapAchievementBean>, TapError> action) {
            platformWrapper.GetLocalUserAchievementList(action);
        }

        public static void FetchUserAchievementList(Action<List<TapAchievementBean>, TapError> action) {
            platformWrapper.FetchUserAchievementList(action);
        }

        public static Task<List<TapAchievementBean>> FetchAllAchievementList() {
            return platformWrapper.FetchAllAchievementList();
        }

        public static Task<List<TapAchievementBean>> GetLocalUserAchievementList() {
            return platformWrapper.GetLocalUserAchievementList();
        }

        public static Task<List<TapAchievementBean>> GetLocalAllAchievementList() {
            return platformWrapper.GetLocalAllAchievementList();
        }

        public static Task<List<TapAchievementBean>> FetchUserAchievementList() {
            return platformWrapper.FetchUserAchievementList();
        }

        public static void Reach(string id) {
            platformWrapper.Reach(id);
        }

        public static void GrowSteps(string id, int step) {
            platformWrapper.GrowSteps(id, step);
        }

        public static void MakeSteps(string id, int step) {
            platformWrapper.MakeSteps(id, step);
        }

        public static void ShowAchievementPage() {
            platformWrapper.ShowAchievementPage();
        }

        public static void SetShowToast(bool showToast)
        {
            platformWrapper.SetShowToast(showToast);
        }

        public static void SetApplicationName(string applicationName)
        {
            platformWrapper.SetApplicationName(applicationName);
        }
        
        public static void SetApplicationIcon(Texture2D applicationIcon)
        {
            platformWrapper.SetApplicationIcon(applicationIcon);
        }

    }
}